// Copyright (c) 2007-2008, 
// Trevor McCauley, http://www.senocular.com, 
// Alessandro Crugnola, http://www.sephiroth.it,
// Marc Speck, http://www.faindu.com
// All Rights Reserved. The following is Source Code and is subject to all restrictions 
// on such code as contained in the license accompanying this product.
package com.faindu.editImage.rotation
{

import com.faindu.editImage.base.BaseControl;

import flash.display.DisplayObject;
import flash.events.Event;
import flash.geom.Point;

/**
 * The user drags these "buttons" to edit the image.
 */
public class RotateControl extends BaseControl
{
	/**
	 * Constructor
	 */
	public function RotateControl( interactionMethod:Function ) 
	{
		super( interactionMethod, "rotate" )
		circle = new ScaleCircle()
		addChild( circle )
		cursor = new RotateCursor( "rotate" )
	}

	//--------------------------------------------------------------------------
    //
    //  Variables
    //
    //--------------------------------------------------------------------------	
	[Embed(source="/com/faindu/editImage/assets/TransformHalloween.swf", symbol="ScaleCircle")]
	private var ScaleCircle:Class;
	
	private var length: Number = 20; 
	private var circle: DisplayObject
	
	/**
	 * @inheritDoc
	 */
	override public function draw( event:Event=null ): void 
	{
		super.draw()
		
		// move circle to point
		const size: Number  = editImage.controlSize
		const size2: Number = size / 2
		graphics.clear()
		
		
		// draw connecting line
		graphics.lineStyle( 2, 0 )
		graphics.lineTo( 0, length )
		
		// invisible square for larger mouse target
  		graphics.moveTo( 0, 0 )
  		graphics.lineStyle()							
		graphics.beginFill( 0xff0000, 0 )
		graphics.drawRect( -size2, 0, size, length )
		graphics.endFill() 
		
		rotateControl( new Point( x, y ) )
	}
	
	/**
	 * @inheritDoc
	 */
 	override public function position():void
	{
		var circlePosition: Point = circlePosition( editImage.controlsPositions.top, 
													editImage.controlsPositions.bottom )
		x = circlePosition.x
		y = circlePosition.y
	}
	 
	/**
	 * Determine the position of the center of the round rotate "button".
	 */
	protected function circlePosition( top:Point, bottom:Point ): Point
	{
		var diff: Point = top.subtract( bottom )
		var angle: Number = Math.atan2( diff.y, diff.x )
		
		return new Point( top.x + length * Math.cos( angle ),
											top.y + length * Math.sin( angle ) )
	}
}
}